﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;

namespace GPS.Controllers
{
    public abstract class GPS_GenericController<T, TRepo, M> : Controller
        where T : class,new()
        where TRepo: IGPSGenericRepository<T>,new()
        where M:class,new()
    {
        protected virtual int PageSize
        {
            get { return 5; }
        }

        protected IGPSGenericRepository<T> _repository;

        public GPS_GenericController() 
        {
            _repository = new TRepo();
        }

        public virtual ActionResult Index( M SearchModel, int? currentPage)
        {
            
            var entities = _repository.SelectAll();
            var model = new List<T>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<T>(currentEntity));
            }

            int pageNumber = (currentPage ?? 1);
            return View(model.ToPagedList(pageNumber, PageSize));
        }

        //public virtual ActionResult Index(int? currentPage)
        //{
        //    var entities = _repository.SelectAll();
        //    var model = new List<T>();
        //    foreach (var currentEntity in entities)
        //    {
        //        model.Add(Mapper.Map<T>(currentEntity));
        //    }

        //    int pageNumber = (currentPage ?? 1);
        //    return View(model.ToPagedList(pageNumber, PageSize));
        //}

        public virtual ActionResult Details(int id)
        {
            var model = _repository.SelectById(id);
            return View(model);
        }

        [HttpGet]
        public virtual ActionResult Create()
        {
            return View(new T());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public virtual ActionResult Create(T model)
        {
            if (ModelState.IsValid)
            {
                _repository.Insert(Mapper.Map<T>(model));
                _repository.Save();
                return RedirectToAction("Index");
            }

            return View(model);
        }

        //[HttpGet]
        //public virtual ActionResult Edit(object id)
        //{
        //    T domainModelEntity = _repository.SelectById(id);
        //    T model = Mapper.Map<T>(domainModelEntity);

        //    return View(model);
        //}

        //[HttpGet]
        //public virtual ActionResult Edit(object id1, object id2)
        //{
        //    T domainModelEntity = _repository.SelectById(id1,id2);
        //    T model = Mapper.Map<T>(domainModelEntity);

        //    return View(model);
        //}

        //[HttpGet]
        //public virtual ActionResult Edit(object id1, object id2, object id3)
        //{
        //    T domainModelEntity = _repository.SelectById(id1, id2,id3);
        //    T model = Mapper.Map<T>(domainModelEntity);

        //    return View(model);
        //}

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public virtual ActionResult Edit(T model)
        {
            if (ModelState.IsValid)
            {
                _repository.Update(Mapper.Map<T>(model));
                _repository.Save();
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public virtual ActionResult Delete(object id)
        {
            _repository.Delete(id);
            _repository.Save();
            return RedirectToAction("Index");
        }
	}
}